﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.Model.SearchModels;
using DormMS.Model;
using Infrastructure;
using DormMS.IService.Common;
using Infrastructure.Tool;

namespace DormMS.Controllers
{
    public class MemberController : BaseController
    {
        protected IMemberService memberService;
        protected ILogService logService;
        public MemberController(IUserInfoService s, IMemberService _memberService,ILogService l) : base(s)
        {
            memberService = _memberService;
            logService = l;
        }

        #region 会员
        // GET: Member
        public ActionResult MemberList()
        {
            return View();
        }


        public ActionResult MemberDetail()
        {
            return View();
        }

        public ActionResult GetMemberInfo(int Id)
        {
            var model = memberService.GetMemberInfo(Id);
            return Json(model);
        }

        public ActionResult SaveMember(Member model)
        {
            Result result = new Result();
            var entity = memberService.GetMemberInfo(model.Id);
            if (entity != null)
            {
                entity.Sex = model.Sex;
                entity.Mobile = model.Mobile;
                entity.RealName = model.RealName;

                entity.EditUserId = CurrentLoginUser.Id;
                entity.EditTime = DateTime.Now;
                if (memberService.EditMember(entity))
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.msg = "更新失败";
                }
            }
            else
            {
                result.msg = "会员不存在";
            }
            return Json(result);
        }

        [HttpPost]
        public ActionResult GetMemberList(MemberSearchModel search)
        {
            int total = 0;
            var list = memberService.GetMemberList(search, ref total);
            return Json(new { Total = total, Results = list });
        }
        #endregion

        #region 会员等级
        public ActionResult LevelList()
        {
            return View();
        }


        [HttpPost]
        public ActionResult GetLevelList()
        {
            var list = memberService.GetMemberLevelList();
            return Json(new { Results = list });
        }

        public ActionResult LevelDetail()
        {
            return View();
        }

        public ActionResult GetLevelInfo(int Id)
        {
            var model = memberService.GetMemberLevelInfo(Id);
            return Json(model);
        }


        public ActionResult EditLevel(MemberLevel model)
        {
            Result result = new Result();

            
            var entity = memberService.GetMemberLevelInfo(model.Id);
            if (model.Amount < 0)
            {
                result.msg = "消费金额不能小于0";
            }
            else if (entity != null)
            {
                entity.LevelName = model.LevelName;
                entity.MemberDayTimes = model.MemberDayTimes;
                entity.OrderPoint = model.OrderPoint;
                entity.SignPoint = model.SignPoint;
                entity.Amount = model.Amount;
                entity.Discount = model.Discount;
                entity.Explain = model.Explain;
                entity.EditUserId = CurrentLoginUser.Id;
                entity.EditTime = DateTime.Now;
                if (memberService.EditMemberLevel(entity))
                {
                    result.isSuccess = true;
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑用户等级", "编辑成功", Generator.GetIP());
                }
                else
                {
                    result.msg = "更新失败";
                    logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑用户等级", "更新失败", Generator.GetIP());
                }
            }
            else
            {
                result.msg = "等级不存在";
                logService.OperationLog(CurrentLoginUser.Id, CurrentLoginUser.UserName, Request.RawUrl,
                            "编辑用户等级", "等级不存在", Generator.GetIP());
            }
            return Json(result);
        }

        #endregion

        #region 充值记录
        public ActionResult PayList()
        {
            return View();
        }


        public JsonResult GetPayList(RechargeSearchModel search)
        {
            Result result = new Result();

            try
            {
                int total = 0;

                result.data= memberService.GetMemberRechargeList(search, ref total);
                result.count = total;
                result.isSuccess = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
                
            }

            return Json(result);
        }
    

        #endregion

        #region 会员地址

        public ActionResult Address()
        {
            return View();
        }

        public ActionResult GetAddress(AddressSearchModel search)
        {
            int total = 0;
            var list = memberService.GetV_MemberAdressList(search,ref total);
            return Json(new { Results = list, Total = total });
        }

        #endregion

        #region 会员资金

        public ActionResult Recharge()
        {
            return View();
        }

        public ActionResult AddRecharge()
        {
            return View();
        }


        public JsonResult GetMemberBalanceDetailList(BaDetailSearchModel search)
        {
            Result result = new Result();

            try
            {
                int total = 0;

                result.data = memberService.GetMemberBalanceDetailList(search, ref total);
                result.count = total;
                result.isSuccess = true;

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }

            return Json(result);
        }

        public JsonResult SaveMemberBalanceDetail(MemberBalanceDetail model,int pointType,int amountType)
        {
            Result result = new Result();

            try
            {
                if (model.Amount == 0 && model.Point == 0)
                {
                    result.msg = "金额和积分不能都为0";
                }
                else
                {
                    model.AddUserId = CurrentLoginUser.Id;
                    if (model.Point != 0)
                    {
                        model.Point = model.Point * pointType;
                    }
                    if (model.Amount != 0)
                    {
                        model.Amount = model.Amount * amountType;
                    }

                    memberService.AddMemberBalanceDetail(model);
                    result.isSuccess = true;
                }

            }
            catch (Exception ex)
            {
                result.msg = ex.Message;

            }

            return Json(result);
        }

        #endregion
    }
}